package com.palm.hubo2o.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.Toast;

import com.palm.hubo2o.BaseFragment;
import com.palm.hubo2o.R;
import com.palm.hubo2o.event.IssueHistoryEvent;
import com.palm.hubo2o.model.Issue;
import com.palm.hubo2o.model.LazyResBody;
import com.palm.hubo2o.model.ScrollerAction;
import com.palm.hubo2o.network.PalmCallBack;
import com.palm.hubo2o.network.PalmRetrofit;
import com.palm.hubo2o.network.Request;
import com.palm.hubo2o.network.Result;
import com.palm.hubo2o.ui.D3LotteryActivity;
import com.palm.hubo2o.ui.K3LotteryActivity;
import com.palm.hubo2o.ui.PrizeListActivity;
import com.palm.hubo2o.ui.QLCLotteryActivity;
import com.palm.hubo2o.ui.SSQLotteryActivity;
import com.palm.hubo2o.util.EnumCode;
import com.palm.hubo2o.util.IssueCenter;
import com.palm.hubo2o.view.HomeHeader;
import com.palm.hubo2o.view.PrizeView;
import com.palm.palmutils.widget.CellView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import retrofit2.Call;


/**
 * Created by dest1 on 2015/10/20.
 */
public class HallFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout mRefreshLayout;
    @Bind(R.id.header_hall)
    HomeHeader mHeader;
    @Bind({R.id.cell_hall_ssq, R.id.cell_hall_qlc, R.id.cell_hall_k3, R.id.cell_hall_3d})
    List<CellView> mCellViews;
    @Bind({R.id.prize_hall_ssq, R.id.prize_hall_qlc, R.id.prize_hall_k3, R.id.prize_hall_3d})
    List<PrizeView> mPrizeViews;

    @Override
    protected int layoutId() {
        return R.layout.fragment_hall;
    }

    @Override
    public void onCreateView2(View view, Bundle savedInstanceState) {
        mRefreshLayout.setColorSchemeResources(R.color.red, R.color.blue);
        mRefreshLayout.setOnRefreshListener(this);
        onRefresh();
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
        mHeader.startAutoScroll();
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
        mHeader.stopAutoScroll();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    private void loadGallery() {
        Call<Result<LazyResBody<ScrollerAction>>> call = PalmRetrofit.getInstance().getApi().scrollingNews(new Request<Void>());
        call.enqueue(new PalmCallBack<LazyResBody<ScrollerAction>>(getActivity()) {
            @Override
            public void succeed(LazyResBody<ScrollerAction> list) {
                mHeader.setGallery(list.getList());
                mHeader.startAutoScroll();
                mRefreshLayout.setRefreshing(false);
            }

            @Override
            public void failed(String message) {
                Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
                mRefreshLayout.setRefreshing(false);
            }
        });
    }

    @OnClick({R.id.cell_hall_ssq, R.id.cell_hall_qlc, R.id.cell_hall_k3, R.id.cell_hall_3d})
    public void toLotteryActivity(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.cell_hall_ssq:
                startActivity(new Intent(getActivity(), SSQLotteryActivity.class));
                break;
            case R.id.cell_hall_qlc:
                startActivity(new Intent(getActivity(), QLCLotteryActivity.class));
                break;
            case R.id.cell_hall_k3:
                startActivity(new Intent(getActivity(), K3LotteryActivity.class));
                break;
            case R.id.cell_hall_3d:
                startActivity(new Intent(getActivity(), D3LotteryActivity.class));
                break;
            default:
                break;
        }
    }

    @OnClick({R.id.prize_hall_ssq, R.id.prize_hall_qlc, R.id.prize_hall_k3, R.id.prize_hall_3d})
    public void toPrizesList(View view) {
        Intent i = new Intent(getContext(), PrizeListActivity.class);
        i.putExtra("gameId", (EnumCode.gameId) view.getTag());
        startActivity(i);
    }

    private void loadPool() {
//        PrizePoolReq ppr = new PrizePoolReq();
//        ppr.gameId = EnumCode.gameId.SSQ.name();
//        HttpClient.execute(ppr, new HttpClient.Callback<LotteryPool>() {
//            @Override
//            public void prepare() {
//
//            }
//
//            @Override
//            public void post() {
//
//            }
//
//            @Override
//            public void succeed(Result<LotteryPool> result) {
//                lotteries.get(0).bottomText = "奖池累计" + StringUtils.money(result.getObject().prizePool);
//                mAdapter.updatePool();
//            }
//
//            @Override
//            public void failed(String error) {
//
//            }
//        });
    }

    @Override
    public void onRefresh() {
        ButterKnife.apply(mPrizeViews, PRIZES, true);
        loadGallery();
        loadPool();
    }

    public void onEventMainThread(IssueHistoryEvent event) {
        ButterKnife.apply(mPrizeViews, PRIZES, false);
    }

    static final ButterKnife.Setter<PrizeView, Boolean> PRIZES = new ButterKnife.Setter<PrizeView, Boolean>() {
        @Override
        public void set(PrizeView view, Boolean bool, int index) {
            EnumCode.gameId id = EnumCode.gameId.values()[index];
            view.setTag(id);
            view.setName(id.getName());
            if (!view.isBallsAdded()) view.createBalls(id);
            List<Issue> l = IssueCenter.open().switchIssueList(id);
            if (!l.isEmpty()) {
                final Issue issue = l.get(0);
                view.setIssue(issue.issueNo);
                view.setDate(issue.prizeTime);
                view.setBalls(issue);
            } else {
                view.setName("正在加载");
                if (bool)
                    IssueCenter.open().startLoadHistory(id, true);
            }

        }
    };


}
